<?php
class Member{
	
	function __construct(){
		
	}
	
	function cashcz($data){
        global $conf;		
		$db = new db($conf);
		$info = userinfo();
		$uppic = isset($data['img2'])?trim($data['img2']):'';
		$cash = isset($data['cash'])?(float)$data['cash']:0;
		if(!$uppic){ error('请上传打款截图');}
		if($cash<=0){ error('请输入充值金额');}
	    $czinfo = $db->field(['*'])->where("uid=".$info['Id']." and state=0")->order("Id desc")->limit("0,1")->select("sc_cashcz");
	    if($czinfo){ error('您还有充值订单未审核 暂无法提交新订单');}
	    $set = set();
	    $up = $db->insert("sc_cashcz",['uid'=>$info['Id'],'cash'=>$cash,'addtime'=>date("Y-m-d H:i:s"),'uppic'=>$uppic,'skpic'=>$set['skpic']]);
	    if($up){
	        success('提交成功');
	    }else{
	        error('提交失败');
	    }
    }

	function newsadd($data){
		global $conf;	
		$db = new db($conf);
		$info = userinfo();
		$title = isset($data['title'])?trim($data['title']):"";
		$cid = isset($data['menu2'])?intval($data['menu2']):0;
		$pic1 = isset($data['img'])?$data['img']:"";		
		$content = isset($data['content'])?htmlspecialchars($data['content']):"";		
		if(strlen($title)<2 || strlen($title)>60){ error("请输入分享标题20字以内");}
		if($cid<1){ error("请选择所属栏目");}
		$cat = $db->field(array("*"))->where("Id=$cid and menu=1")->select("sc_menu");
		if(!$cat){ error("所选栏目不存在");}
		$pic = '';
		if(is_array($pic1) && count($pic1)>0 && $pic1[0]){
			$pic = $pic1[0];
		}else{
			error('请上传图片');
		}
		if(strlen($content)<2 || strlen($content)>300){ error("请输入分享内容100字以内");}
		$pic1 = json_encode($pic1);
		$db->insert("sc_news",array("title"=>$title,"cid"=>$cid,"uid"=>$info['Id'],"lx"=>$cat[0]['lx'],"pic"=>$pic,"pic1"=>$pic1,"content"=>$content,"state"=>0,"addtime"=>date("Y-m-d H:i:s")));
		success("上传成功 客服将尽快审核");
	}

	function upload($data){
		global $conf;	
		$db = new db($conf);
		$info = userinfo();
		$ninfo = $db->field(['*'])->where("uid=".$info['Id']." and state=0")->select("sc_news");
		if($ninfo){ error('分享信息正在审核中');}
		$year = date('Y',time());
		$yue = date('m',time());
		$ri = date('d',time());
		$riqi = date('md',time());
		$base64_img = iconv('UTF-8','gb2312',$data['img']);
		if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
			$filetype = $result[2];
			if($filetype=="jpeg"){
				$filetype = "jpg";
			}
			if($filetype!='jpg' && $filetype!='gif' && $filetype!='png'){ error('ERROR');}
			$rq = $riqi.time().rand(1000,9999);
			if(!file_exists("upload/$year$yue$ri")){
				$qx = umask(0);
				mkdir("upload/$year$yue$ri",0777,true);
				umask($qx);
				$new_file = "upload/$year$yue$ri/" .$rq.".".$filetype;
				if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
					$imgpath = "/upload/$year$yue$ri/" .$rq.".".$filetype;
					$response['0'] = 1;    
					$response['1'] = $imgpath;
					echo json_encode($response); 
				}else{				
					error("上传失败");
				}		
			}else{
				$new_file = "upload/$year$yue$ri/" .$rq.".".$filetype;
				if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
					$imgpath = "/upload/$year$yue$ri/" .$rq.".".$filetype;
					$response['0'] = 1;    
					$response['1'] = $imgpath;
					echo json_encode($response); 
				}else{				
					error("上传失败");
				}
			}	
		}else{
			error("上传失败");
		}
	}

	function n_random(){
		global $conf;	
		$db = new db($conf);
		do{
			$str = getRandom1(2).getRandom(6);
			$r1 = $db->field(array("Id"))->where("uname='".$str."'")->select("sc_member");			
		}while($r1);
		success($str);
	}

	function searchm($data){
		global $conf;	
		$db = new db($conf);
		$uname = isset($data['cardid'])?trim($data['cardid']):'';
		if(strlen($uname)>7){
			$info = $db->field(array("tname"))->where("uname='".$uname."'")->select("sc_member");
			if($info){
				success($info[0]['tname']);
			}
		}
	}

	function cashzh($data){
		global $conf;	
		$db = new db($conf);
		$cash = isset($data['t2'])?(float)$data['t2']:0;
		if($cash<=0){ error("请输入正确的转换金额");}		
		$info = userinfo();
		if($info['jjcash']<$cash){ error("福豆不足");}
		$up = $db->where("Id=".$info['Id'])->update("sc_member",array("cash"=>"(cash + $cash)","jjcash"=>"(jjcash - $cash)"));
		if($up){
			$db->insert("sc_cash",array("uid"=>$info['Id'],"cash"=>$cash,"addtime"=>date("Y-m-d H:i:s"),"type"=>10));
			$db->insert("sc_spend",array("uid"=>$info['Id'],"cash"=>$cash,"type"=>8,"addtime"=>date("Y-m-d H:i:s"),"adda"=>1,"pay"=>1));
			success("转换成功");
		}else{
			error("转换失败");
		}
	}



function tixian($data){
	global $conf;	
	$db = new db($conf);
	
	$cash = isset($data['t4'])?$data['t4']:0;
	$phone = isset($data['t1'])?$data['t1']:"";
	$alipay = isset($data['t2'])?$data['t2']:"";
	$wechat = isset($data['t3'])?$data['t3']:"";
	$type = isset($data['type'])?$data['type']:0;
	$bank = isset($data['bank'])?intval($data['bank']):0;
	$set = set();
	if($set['txlimit']){	
		if(!$cash || !preg_match("/^\d*$/",$cash) || $cash<=0 || $cash%$set['txlimit']!=0){
			error("提现金额为".$set['txlimit']."的整数倍");
		}
	}else{
		if(!$cash || !preg_match("/^\d*$/",$cash) || $cash<=0){
			error("请输入正确的提现金额");
		}
	}
	
	$info = userinfo();
	if(!$info){ error("信息错误");}
	$r1 = $db->field(array("*"))->where("Id=$bank and uid=".$info['Id'])->select("sc_bank");
	if(!$r1){ error("请填写收款方式");}	
	
	if($info['jjcash']<$cash){ error("可提现收益不足");}	
	$content = "收益提现";	
		
	$field = array("uid"=>$info['Id'],"content"=>$content,"num"=>$cash,"cash"=>round($cash*(100 - $set['tixian'] - $set['tixian1'])/100,2),"addtime"=>date("Y-m-d H:i:s",time()),"alipay"=>$alipay,"type"=>$type,"weixin"=>$wechat,"phone"=>$phone,"bname"=>$r1[0]['bankname'],"bcard"=>$r1[0]['bankcard'],"btname"=>$r1[0]['truename'],"sxcash1"=>round($cash*$set['tixian']/100,2),"sxcash2"=>round($cash*$set['tixian1']/100,2));	
	$up = $db->where("Id=".$info['Id'])->update("sc_member",array("jjcash"=>"(jjcash - $cash)"));		
	if($up){		
		$db->insert("sc_cash",array("uid"=>$info['Id'],"cash"=>$cash,"content"=>$content,"type"=>11,"addtime"=>date("Y-m-d H:i:s")));			
		$result = $db->insert("sc_tixian",$field);
		if($result){			
			success("提交成功，请等待审核");
		}else{
			error("提现失败");	
		}
	}else{
		error("提现失败");
	}	
}
//添加银行卡
function bank_add($data){
	global $conf;
	$db = new db($conf);
	$bankname = isset($data['t1'])?trim($data['t1']):"";
	$bankcard = isset($data['t2'])?trim($data['t2']):"";
	$truename = isset($data['t3'])?trim($data['t3']):"";
	if(!$bankname){ error("请输入开户行");}
	if(!$bankcard){ error("请输入银行卡号");}
	if(!$truename){ error("请输入持卡人姓名");}
	$db->insert("sc_bank",array("uid"=>$_SESSION['userid'],"bankname"=>$bankname,"bankcard"=>$bankcard,"truename"=>$truename,"type"=>0,"state"=>1));
	$db->insert("sc_bank_record",['uid'=>$_SESSION['userid'],'content'=>'新增：'.$bankname.'['.$bankcard.']'.$truename,'addtime'=>date('Y-m-d H:i:s')]);
	success("添加成功");
}
//编辑银行卡
function bank_edit($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if(!$id || $id != $data['id']){ error("通信异常 请重试");}
	$bankname = isset($data['t1'])?trim($data['t1']):"";
	$bankcard = isset($data['t2'])?trim($data['t2']):"";
	$truename = isset($data['t3'])?trim($data['t3']):"";
	if(!$bankname){ error("请输入开户行");}
	if(!$bankcard){ error("请输入银行卡号");}
	if(!$truename){ error("请输入持卡人姓名");}
	$info = $db->field(['*'])->where("Id=$id and uid=".$_SESSION['userid'])->select("sc_bank");
	if(!$info){
		error('未获取到银行卡信息');
	}
	$db->where("Id=$id and uid=".$_SESSION['userid'])->update("sc_bank",array("uid"=>$_SESSION['userid'],"bankname"=>$bankname,"bankcard"=>$bankcard,"truename"=>$truename,"state"=>1));
	$db->insert("sc_bank_record",['uid'=>$_SESSION['userid'],'content'=>'原信息：'.$info[0]['bankname'].'['.$info[0]['bankcard'].']'.$info[0]['truename'].'<br />新纪录：'.$bankname.'['.$bankcard.']'.$truename,'addtime'=>date('Y-m-d H:i:s')]);
	success("编辑成功");
}

//删除银行卡信息
function bank_del($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if(!$id || $id != $data['id']){ error("通信异常");}
	$info = $db->field(['*'])->where("Id=$id and uid=".$_SESSION['userid'])->select("sc_bank");
	if(!$info){
		error('未获取到银行卡信息');
	}
	$db->where("Id=$id and uid=".$_SESSION['userid'])->delete("sc_bank");
	$db->insert("sc_bank_record",['uid'=>$_SESSION['userid'],'content'=>'删除：'.$info[0]['bankname'].'['.$info[0]['bankcard'].']'.$info[0]['truename'],'addtime'=>date('Y-m-d H:i:s')]);
	success("删除成功");
}

//设为默认银行卡
function bank_default($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if(!$id || $id != $data['id']){ error("通信异常");}
	$info = $db->field(['*'])->where("Id=$id and uid=".$_SESSION['userid'])->select("sc_bank");
	if(!$info){
		error('未获取到银行卡信息');
	}
	$db->where("uid=".$_SESSION['userid']." and type=1")->update("sc_bank",array("type"=>0));
	$db->where("Id=$id and uid=".$_SESSION['userid'])->update("sc_bank",array("type"=>1));
	$db->insert("sc_bank_record",['uid'=>$_SESSION['userid'],'content'=>'设置默认：'.$info[0]['bankname'].'['.$info[0]['bankcard'].']'.$info[0]['truename'],'addtime'=>date('Y-m-d H:i:s')]);
	success("设置成功");
}
	
//删除收货地址
function add_del($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if(!$id || $id != $data['id']){ error("通信异常");}
	$db->where("Id=$id and uid=".$_SESSION['userid'])->delete("sc_address");
	success("删除成功");
}

//设为默认收货地址
function add_default($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	if(!$id || $id != $data['id']){ error("通信异常");}
	$db->where("uid=".$_SESSION['userid']." and type=1")->update("sc_address",array("type"=>0));
	$db->where("Id=$id and uid=".$_SESSION['userid'])->update("sc_address",array("type"=>1));
	success("设置成功");
}

//修改收货地址
function address_edit($data){
	global $conf;
	$db = new db($conf);
	$id = isset($data['id'])?intval($data['id']):0;
	$province = isset($data['province'])?intval($data['province']):0;
	$city = isset($data['city'])?intval($data['city']):0;
	$county = isset($data['county'])?intval($data['county']):0;
	$address = isset($data['t10'])?trim($data['t10']):"";
	$tname = isset($data['t11'])?trim($data['t11']):"";
	$phone = isset($data['t12'])?trim($data['t12']):"";
	$is_default = isset($data['is_default'])?intval($data['is_default']):0;
	if(!$id){error("页面信息异常，请刷新");}
	if(!$tname){ error("请填写收件人姓名");}
	$preg_m = "/^1[3456789]\d{9}$/";
	if(strlen($phone)!=11 || !preg_match($preg_m, $phone)){
		error("请输入正确的11位手机号码");
	}
	//if(strlen($phone)!=11 || ){ error("请填写收件人联系电话");}
	if(!$province){ error("请选择所在省份");}
	if(!$city){ error("请选择所在市区");}
	if(!$county){ error("请选择所在区县");}
	if(!$address){ error("请填写详细收货地址");}

	// 如果设置为默认地址，先清除其他默认地址
	if($is_default == 1){
		$db->where("uid=".$_SESSION['userid']." and type=1")->update("sc_address",array("type"=>0));
	}
	
	$field = array("province"=>$province,"city"=>$city,"county"=>$county,"address"=>$address,"tname"=>$tname,"phone"=>$phone,"type"=>$is_default,"state"=>1);
	$db->where("Id=$id and uid=".$_SESSION['userid'])->update("sc_address",$field);
	success("修改成功");
}

//添加收货地址
function address_add($data){
	global $conf;
	$db = new db($conf);
	$province = isset($data['province'])?intval($data['province']):0;
	$city = isset($data['city'])?intval($data['city']):0;
	$county = isset($data['county'])?intval($data['county']):0;
	$address = isset($data['t10'])?trim($data['t10']):"";
	$tname = isset($data['t11'])?trim($data['t11']):"";
	$phone = isset($data['t12'])?trim($data['t12']):"";
	$is_default = isset($data['is_default'])?intval($data['is_default']):0;
	if(!$tname){ error("请填写收件人姓名");}
	$preg_m = "/^1[3456789]\d{9}$/";
	if(strlen($phone)!=11 || !preg_match($preg_m, $phone)){
		error("请输入正确的11位手机号码");		
	}
	//if(!$phone){ error("请填写收件人联系电话");}
	if(!$province){ error("请选择所在省份");}
	if(!$city){ error("请选择所在市区");}
	if(!$county){ error("请选择所在区县");}
	if(!$address){ error("请填写详细收货地址");}
	//用户个人信息
	$r1 = $db->field(array("*"))->where("Id=".$_SESSION['userid']." and state=1")->select("sc_member");
	if(!$r1){ error("通信异常");}

	// 如果设置为默认地址，先清除其他默认地址
	if($is_default == 1){
		$db->where("uid=".$_SESSION['userid']." and type=1")->update("sc_address",array("type"=>0));
	}

	$field = array("uid"=>$_SESSION['userid'],"province"=>$province,"city"=>$city,"county"=>$county,"address"=>$address,"tname"=>$tname,"phone"=>$phone,"type"=>$is_default,"state"=>1);
	$result = $db->insert("sc_address",$field);
	if($result){		
		success("添加成功");
	}else{
		error("添加失败");
	}
}
//修改会员信息
function useredit($data){
	global $conf;
	global $results;
	$db = new db($conf);
	$id = intval($_SESSION['userid']);
	//$uname = trim($_SESSION['uname']);
	$tname = isset($data['xm'])?trim($data['xm']):"";
	$yqcode = isset($data['yqcode'])?trim($data['yqcode']):"";
	if(!pregmatch($tname,'name')){ error('请输入姓名 2-4个中文字符');}
	$info = $db->field(array("*"))->where("Id=$id and state=1")->select("sc_member");
	$field = array("tname"=>$tname);
	if(strlen($yqcode)>0){
		if($info[0]['tjid']>0){
			error('您已绑定推荐人 请勿重复操作');
		}
		if(strlen($yqcode)!=6){ error('请输入6位邀请码');}
		$tinfo = $db->field(array("*"))->where("yqcode='".$yqcode."' and state=1")->select("sc_member");
		if(!$tinfo){ error("未查询到推荐人信息");}
		if($tinfo[0]['level']<1){ error("推荐人无权限");}
		$tinfo = $tinfo[0];
		$jiedian = $tinfo['Id'].",".$tinfo['jiedian'];
		$field['tjid'] = $tinfo['Id'];
		$field['tjname'] = $tinfo['uname'];
		$field['jiedian'] = $jiedian;
		$db->where("Id in($jiedian)")->update("sc_member",array("teamnum"=>"(teamnum + 1)"));	
	}	
	
	$where = array("Id"=>$id,"state"=>1);	
	$result = $db->where($where)->update("sc_member",$field);
	if($result){
		success("修改成功");
	}else{
		error("修改会员失败");
	}	
}

//获取当前省所辖市区
function city($data){
	global $conf;
	$db = new db($conf);
	$areaid = isset($data['id'])?intval($data['id']):0;
	if(!empty($areaid)){		
		$area = $db->field(array("areaid","areaname"))->where(array("areapid"=>$areaid))->select("sc_area");
		if($area){
			$str = "<option value='0' selected>-请选择-</option>";
			for($i=0;$i<count($area);$i++){
				$str = $str."<option value='".$area[$i]['areaid']."'>".$area[$i]['areaname']."</option>";
			}
			echo json_encode(array(1,$str));
		}		
	}
}

//获取当前市所辖县区
function province($data){
	global $conf;
	$db = new db($conf);
	$areaid = isset($data['id'])?intval($data['id']):0;
	if(!empty($areaid)){
		$area = $db->field(array("areaid","areaname"))->where(array("areapid"=>$areaid))->select("sc_area");
		
		if($area){
			$str = "<option value='0' selected>-请选择-</option>";
			for($i=0;$i<count($area);$i++){
				$str = $str."<option value='".$area[$i]['areaid']."'>".$area[$i]['areaname']."</option>";
			}
			echo json_encode(array(1,$str));
		}		
	}
}

//修改密码
function getpass($data){
	global $conf;
	$db = new db($conf);
	$old = isset($data['t0'])?$data['t0']:"";
	$new = isset($data['t1'])?$data['t1']:"";
	$new1 = isset($data['t2'])?$data['t2']:"";
	if(empty($old)){
		error("请输入原密码");
	}
	if(empty($new)){
		error("请输入新密码");
	}
	if(empty($new1)){
		error("请再次输入新密码");
	}
	if(strlen($new1)!=strlen($new) || $new1 != $new){
		error("两次输入密码不一致");
	}
	$password = MD5($new);
	$password1 = MD5($old);
	$r1 = $db->field(array("Id"))->where(array("Id"=>"'".$_SESSION['userid']."'","pwd1"=>"'".$password1."'"))->select("sc_member");
	if($r1){
		$field = array("pwd1"=>$password);
		$db->where(array("Id"=>"'".$r1[0]['Id']."'"))->update("sc_member",$field);
		success("修改成功");
	}
	else{
		error("原密码错误");
	}
}

function huzhuan($data){
	global $conf;
	$db = new db($conf);
	$uid = $_SESSION['userid']; 
	$runame = isset($data['t1'])?trim($data['t1']):"";
	$cash = isset($data['t2'])?intval($data['t2']):0;
	$type = isset($data['type'])?intval($data['type']):0;
	$set = set();
	if($type!=1){ error("未知错误");}
	if(empty($runame)){error("请输入转入账户");}
	if(!$cash || $cash<=0 || $cash != $data['t2'] || $cash%100!=0){error("请输入正确的转入数量 100的倍数");}
	//获取转出人信息
	$r1 = $db->field(array("*"))->where("Id=$uid and state=1")->select("sc_member");
	if(!$r1){error("当前账户异常");}
	if($r1[0]['hzlimit']==1){ error('当前账户互转功能未开启');}
	//获取转入人信息
	$r2 = $db->field(array("*"))->where("uname='".$runame."' and find_in_set(".$r1[0]['Id'].",jiedian) and state=1")->select("sc_member");
	if(!$r2){ error("转入账户不存在");}
	if($r2[0]['hzlimit']==1){ error('转入账户互转功能未开启');}
	if($type==1){
		if($set['hzsxf']>0){
			$sxf = round($cash*$set['hzsxf']/100,2);
		}else{
			$sxf = 0;
		}
		if($r1[0]['cash']<$cash + $sxf){ error("余额不足");}
		$up1 = $db->where("Id=$uid")->update("sc_member",array("cash"=>"(cash - $cash)"));
	}
	if($up1){
		$field3 = array("uid"=>$uid,"rid"=>$r2[0]['Id'],"cash"=>$cash,"addtime"=>date("Y-m-d H:i:s",time()),"type"=>$type,"rcash"=>$cash,"state"=>1,"content"=>"转入".$r2[0]['tname']);
		$db->insert("sc_huzhuan",$field3);
		if($type==1){
			$db->insert("sc_spend",array("uid"=>$uid,"cash"=>$cash,"content"=>"转入".$r2[0]['tname'],"addtime"=>date("Y-m-d H:i:s",time()),"type"=>2,"pay"=>1));
			$db->insert("sc_spend",array("uid"=>$r2[0]['Id'],"cash"=>$cash,"content"=>$r1[0]['tname']."转入","addtime"=>date("Y-m-d H:i:s",time()),"type"=>3,"adda"=>1,"pay"=>1));
			$up1 = $db->where("Id=".$r2[0]['Id'])->update("sc_member",array("cash"=>"(cash + $cash)"));		
			if($sxf>0){
				$db->where("Id=$uid")->update("sc_member",array("cash"=>"(cash - $sxf)"));
				$db->insert("sc_spend",array("uid"=>$uid,"cash"=>$sxf,"content"=>"转出手续费","addtime"=>date("Y-m-d H:i:s"),"type"=>9,"pay"=>1));
			}
		}
		
		success("转出成功");
	}else{
		error("转出失败");
	}
	
	
	
}


//添加会员
 function reg($data){
	global $conf;
	global $results;
	$db = new db($conf);
	$uname = isset($data['t0'])?trim($data['t0']):"";
	$phone = isset($data['t1'])?trim($data['t1']):"";
	$code = isset($data['code'])?trim($data['code']):"";
	$tname = isset($data['t3'])?trim($data['t3']):"";
	$password = isset($data['t4'])?trim($data['t4']):"";
	$password1 = isset($data['t5'])?trim($data['t5']):"";
	$tjname = isset($data['t7'])?trim($data['t7']):"";
	$date = isset($data['date'])?trim($data['date']):"";
	$code = isset($data['code'])?$data['code']:"";
	
	if(!preg_match("/^[a-z\d]{8,16}$/i",$uname)){error("账户为8-16位任意字母数字");}
	$r1 = $db->field(array("*"))->where("uname='".$uname."'")->select("sc_member");
	if($r1){ error("账户名已注册 请更换");}
	if(strlen($phone)!=11){ error("请输入手机号 11 位");}

	if(!isset($_SESSION['code'])){error("请获取手机验证码");}
	if((time()-$_SESSION['yzmtime'])>600){error("短信验证码已失效，请重新获取");}
	if($code!=$_SESSION['code']){error("短信验证码错误");}
	if($phone != $_SESSION['mobile']){error("短信验证码不匹配");}
	
	if(!pregmatch($tname,'name')){ error('请输入姓名 2-4个中文字符');}
	
	if(empty($password) || strlen($password)<6 || strlen($password)>18){ error("请输入6-18位会员密码"); }
	if(empty($password1) || strlen($password1)<6 || strlen($password1)>18){ error("请再次输入相同的6-18位会员密码"); }
	if($password != $password1){ error("两次输入密码不一致"); }		
	if($tjname){
		$tinfo = $db->field(array("*"))->where("yqcode='".$tjname."' and state=1")->select("sc_member");
		if(!$tinfo){ error("未查询到推荐人信息");}
		if($tinfo[0]['level']<1){ error("推荐人无权限");}
	}else{
		error('请输入邀请码');
	}

	$rphone = $db->field(array("count(*)"))->where(array("phone"=>"'".$phone."'"))->select("sc_member");
	if($rphone[0]['count(*)']>3){error("同一手机号限制注册4个账户");}		
	
	if($tjname){
		$info = $tinfo[0];
		$jiedian = $info['Id'].",".$info['jiedian'];
	}else{
		$info = ['uname'=>'','Id'=>0];
		$jiedian = 0;
	}
	
	do{
		$yqcode = getRandom(6);
		$r1 = $db->field(array("count(*)"))->where("yqcode='".$yqcode."'")->select("sc_member");				
	}while($r1[0]['count(*)']);		
	$set = set();
	
	$add = $db->insert("sc_member",array("tname"=>$tname,"uname"=>$uname,"pwd1"=>MD5($password),"pwd2"=>MD5($password),"tjname"=>$info['uname'],"tjid"=>$info['Id'],"phone"=>$phone,"jiedian"=>$jiedian,"state"=>1,"addtime"=>date("Y-m-d H:i:s",time()),"yqcode"=>$yqcode,"pwdshow"=>$password));
	if($add){
		$id = $db->done("LAST_INSERT_ID()");
		$id = $id[0]['LAST_INSERT_ID()'];
		
		$db->where("Id in($jiedian)")->update("sc_member",array("teamnum"=>"(teamnum + 1)"));		
		unset($_SESSION['code']);
		unset($_SESSION['yzmtime']);
		unset($_SESSION['uname']);
		unset($_SESSION['mobile']);
		$_SESSION['userid'] = $id;
		$_SESSION['tname'] = $tname;
		$_SESSION['uname'] = $uname;
		$_SESSION['state'] = 1;		
		$_SESSION['time'] = time();
		$sjcode = getRandom(12);
		$db->where("Id=$id")->update("sc_member",array("sjcode"=>$sjcode));
		setcookie("username",$uname,time()+3600*24*365,"");
		setcookie("password",$sjcode,time()+3600*24*365,"");
		success("注册成功");
	}else{
		unset($_SESSION['code']);
		unset($_SESSION['yzmtime']);
		unset($_SESSION['uname']);
		unset($_SESSION['mobile']);
		error("注册失败");
	}
}

function ureg($data){
	global $conf;	
	$db = new db($conf);
	$uname = isset($data['t0'])?trim($data['t0']):"";
	$phone = isset($data['t1'])?trim($data['t1']):"";
	$code = isset($data['code'])?trim($data['code']):"";
	$tname = isset($data['t3'])?trim($data['t3']):"";
	$password = isset($data['t4'])?trim($data['t4']):"";
	$password1 = isset($data['t5'])?trim($data['t5']):"";
	$tjname = isset($data['t7'])?trim($data['t7']):"";
	$date = isset($data['date'])?trim($data['date']):"";
	$info = userinfo();
	if($info['level']<1){ error("当前账户权限不足");}	
	if(!preg_match("/^[a-z\d]{8,16}$/i",$uname)){error("账户为8-16位任意字母数字");}
	$r1 = $db->field(array("*"))->where("uname='".$uname."'")->select("sc_member");
	if($r1){ error("账户名已注册 请更换");}
	if(strlen($phone)!=11){ error("请输入手机号 11 位");}
	
	if(!isset($_SESSION['code'])){error("请获取手机验证码");}
	if((time()-$_SESSION['yzmtime'])>600){error("短信验证码已失效，请重新获取");}
	if($code!=$_SESSION['code']){error("短信验证码错误");}
	if($phone != $_SESSION['mobile']){error("短信验证码不匹配");}
	if(!pregmatch($tname,'name')){ error('请输入姓名 2-4个中文字符');}
	
	if(empty($password) || strlen($password)<6 || strlen($password)>18){ error("请输入6-18位会员密码"); }
	if(empty($password1) || strlen($password1)<6 || strlen($password1)>18){ error("请再次输入相同的6-18位会员密码"); }
	if($password != $password1){ error("两次输入密码不一致"); }		
	if($tjname){
		$tinfo = $db->field(array("*"))->where("yqcode='".$tjname."' and state=1")->select("sc_member");
		if(!$tinfo){ error("未查询到推荐人信息");}
		if($tinfo[0]['level']<1){ error("推荐人无权限");}
	}
	
	//再次验证手机号码是否存在
	$rphone = $db->field(array("count(*)"))->where(array("phone"=>"'".$phone."'"))->select("sc_member");
	if($rphone[0]['count(*)']>3){error("同一手机号限制注册4个账户");}	
	if($tjname){
		$info = $tinfo[0];
	}
	$jiedian = $info['Id'].",".$info['jiedian'];
	do{
		$yqcode = getRandom(6);
		$r1 = $db->field(array("count(*)"))->where("yqcode='".$yqcode."'")->select("sc_member");				
	}while($r1[0]['count(*)']);		
	$set = set();
	
	$add = $db->insert("sc_member",array("tname"=>$tname,"uname"=>$uname,"pwd1"=>MD5($password),"pwd2"=>MD5($password),"tjname"=>$info['uname'],"tjid"=>$info['Id'],"phone"=>$phone,"jiedian"=>$jiedian,"state"=>1,"addtime"=>date("Y-m-d H:i:s",time()),"yqcode"=>$yqcode,"pwdshow"=>$password));
	if($add){
		$id = $db->done("LAST_INSERT_ID()");
		$id = $id[0]['LAST_INSERT_ID()'];
		
		$db->where("Id in($jiedian)")->update("sc_member",array("teamnum"=>"(teamnum + 1)"));
		unset($_SESSION['code']);
		unset($_SESSION['yzmtime']);
		unset($_SESSION['uname']);
		unset($_SESSION['mobile']);	
		$str = [1,'注册成功',$id];
		echo json_encode($str);		
	}else{
		unset($_SESSION['code']);
		unset($_SESSION['yzmtime']);
		unset($_SESSION['uname']);
		unset($_SESSION['mobile']);
		error("注册失败");
	}	
	
}

function yzm($data){
	global $conf;
	$db = new db($conf);
	$preg_m = "/^1[3456789]\d{9}$/";
	$uname = isset($data['uname'])?trim($data['uname']):"";
	$mobile = isset($data['mobile'])?trim($data['mobile']):"";
	
	if(isset($_SESSION['yzmtime']) && (time()-$_SESSION['yzmtime'])<60){
		$str = array(0,"验证码60秒发送一次 请稍等");
		echo json_encode($str);exit;
	}
	if(!preg_match("/^[a-z\d]{8,16}$/i",$uname)){error("账户为8-16位任意字母数字");}
	if(strlen($mobile)!=11 || !preg_match($preg_m, $mobile)){
		$str = array(0,"请输入正确的手机号码");
		echo json_encode($str);exit;
	}
	$exit = $db->field(array("*"))->where(array("uname"=>"'".$uname."'"))->select("sc_member");
	if($exit){
		 error("该账号已注册 请更换");
	}
	$rphone = $db->field(array("count(*)"))->where(array("phone"=>"'".$mobile."'"))->select("sc_member");
	if($rphone[0]['count(*)']>3){
		 error("同一手机号限制注册4个账户");
	}
	
	$arrnum = array(1,2,3,4,5,6,7,8,9,0);
	$yzm = "";
	for($i=0;$i<6;$i++){ 
		$zifu = $arrnum[mt_rand(0,9)]; 
		$yzm .= $zifu;
	}
	$set = set();
	if(debug==false && $set['smstype']==1){
		$arr_date = array("accesskey"=>"","secret"=>"","sign"=>"","templateId"=>"281630","mobile"=>$mobile,"content"=>$yzm."##10");
		$url = "https://api.1cloudsp.com/api/v2/single_send";
		$res = r_post1($url,$arr_date);
		$result = json_decode($res,true);
				
		if($result['code'] == 0){ 
			$_SESSION['code'] = $yzm;
			$_SESSION['mobile'] = $mobile;
			$_SESSION['uname'] = $uname;
			$_SESSION['yzmtime'] = time();
			$str = array(1,"验证码已发送");
			echo json_encode($str);
		}
		elseif($result['code']==9024){
			$str = array(0,"今日验证短信次数已用完");
			echo json_encode($str);
		}
		else{
			$str = array(0,"发送失败，请联系管理员");
			echo json_encode($str);
		} 
	}else{
		$_SESSION['code'] = $yzm;
		$_SESSION['mobile'] = $mobile;
		$_SESSION['uname'] = $uname;
		$_SESSION['yzmtime'] = time();
		$str = array(1,$yzm);
		echo json_encode($str);
	}
	      
	
}

function login($data){
	global $conf;
	$uname = isset($data['t0'])?trim($data['t0']):"";
	$upass = isset($data['t1'])?trim($data['t1']):"";
	if(empty($uname)){ error("请输入登录帐号");}
	if(empty($upass)){ error("请输入登录密码");}
	$db =  new db($conf);
	$field = array("*");
	$where = "uname='".$uname."' and pwd1='".MD5($upass)."'";
	$result = $db->field($field)
			->where($where)
			->select("sc_member");
	if($result){
		if($result[0]['uname']!=$uname || $result[0]['pwd1']!=MD5($upass)){
			error("帐号或密码错误");
		}
		if($result[0]['state']==0){
			error("违规操作，帐号已被冻结");
		}
		$_SESSION['userid'] = $result[0]['Id'];
		$_SESSION['tname'] = $result[0]['tname'];
		$_SESSION['uname'] = $result[0]['uname'];
		$_SESSION['state'] = $result[0]['state'];		
		$_SESSION['time'] = time();
		$sjcode = getRandom(12);
		$db->where("Id=".$result[0]['Id'])->update("sc_member",array("sjcode"=>$sjcode));
		setcookie("username",$result[0]['uname'],time()+3600*24*365,"");
		setcookie("password",$sjcode,time()+3600*24*365,"");
		success("登录成功，请稍等");
	}
	else{
		error("帐号或密码错误");
	}
}

function liuyan($data){
	global $conf;
	$db = new db($conf);
	$content = isset($data['t0'])?$data['t0']:"";
	if(empty($content)){
		$str = array("0","请输入留言内容"); echo json_encode($str);exit;
	}
	$field = array("content"=>$content,"uid"=>$_SESSION['userid'],"addtime"=>date("Y-m-d H:i:s",time()),"state"=>0);
	$result = $db->insert("sc_message",$field);
	if($result){
		$str = array(1,"留言已提交，客服会尽快给您回复"); echo json_encode($str);
	}
	else{
		$str = array(0,"留言系统暂忙，请致电客服咨询"); echo json_encode($str);
	}
}

function getpwd($data){
	global $conf;
	$db = new db($conf);
	$phone = isset($data['t0'])?$data['t0']:"";
	$uname = isset($data['u'])?$data['u']:"";
	$yzm = isset($data['t1'])?$data['t1']:"";
	$pwd1 = isset($data['t2'])?$data['t2']:"";
	$pwd2 = isset($data['t3'])?$data['t3']:"";
	 if(empty($uname)){
	 	$str = array(0,"请输入会员账户"); echo json_encode($str);exit;
	 }
	if(empty($phone)){
		$str = array(0,"请输入手机号"); echo json_encode($str);exit;
	}
	if(!isset($_SESSION['code1'])){
		$str = array("0","请获取手机验证码");echo json_encode($str);exit;
	}
	
	if(empty($pwd1) || strlen($pwd1)<6 || strlen($pwd1)>18){ $str = array("0","请输入6-18位会员密码"); echo json_encode($str);exit;}
	if(empty($pwd2) || strlen($pwd2)<6 || strlen($pwd2)>18){ $str = array("0","请再次输入相同的6-18位会员密码"); echo json_encode($str);exit;}
	

	if(strlen($pwd1)!=strlen($pwd2) || $pwd1 != $pwd2){
		$str = array(0,"两次输入密码不一致"); echo json_encode($str);exit;
	}
	
	
	if((time()-$_SESSION['yzmtime1'])>600){
		 $str = array("0","验证码已失效，请重新获取");session_destroy(); echo json_encode($str);exit;
	}
	if($yzm!=$_SESSION['code1']){
		 $str = array("0","验证码错误"); echo json_encode($str);exit;
	}
	if($phone != $_SESSION['mobile1'] || $uname != $_SESSION['uname1']){
		 $str = array("0","验证码不匹配"); echo json_encode($str);exit;
	}
	
	$password = MD5($pwd1);		
	$field = array("pwd1"=>$password);
	$r1 = $db->where(array("phone"=>"'".$phone."'","uname"=>"'".$uname."'","state"=>1))->update("sc_member",$field);
	if($r1){
		$str = array(1,"修改密码成功");
		echo json_encode($str);
	}
	else{
		$str = array(0,"修改密码失败");
		echo json_encode($str);
	}
	unset($_SESSION['code1']);
	unset($_SESSION['yzmtime1']);
	unset($_SESSION['mobile1']);
	unset($_SESSION['uname1']);
}

function czyzm($data){
	global $conf;
	$db = new db($conf);
	$preg_m = "/^1[3456789]\d{9}$/";
	$uname = $data['uname'];
	$mobile = $data['mobile'];
	if(isset($_SESSION['yzmtime']) && (time()-$_SESSION['yzmtime'])<60){
		$str = array(0,"验证码冷却中(60秒)");
		echo json_encode($str);exit;
	}
	if(strlen($uname)<8 || strlen($uname)>16){ error("请输入正确的会员账户");}
	if(strlen($mobile)!=11 || !preg_match($preg_m, $mobile)){
		$str = array(0,"请输入正确的手机号码");
		echo json_encode($str);exit;
	}
	$rphone = $db->field(array("*"))->where(array("phone"=>"'".$mobile."'","uname"=>"'".$uname."'"))->select("sc_member");
	if(!$rphone){
		 $str = array("0","未查询到注册信息"); echo json_encode($str);exit;
	}
	
	$arrnum = array(1,2,3,4,5,6,7,8,9,0);
	$yzm = "";
	for($i=0;$i<6;$i++){
		$zifu = $arrnum[mt_rand(0,9)];
		$yzm .= $zifu;
	}
	$set = set();
	if(debug==false && $set['smstype']==1){
		$arr_date = array("accesskey"=>"","secret"=>"","sign"=>"","templateId"=>"281630","mobile"=>$mobile,"content"=>$yzm."##10");
		$url = "https://api.1cloudsp.com/api/v2/single_send";
		$res = r_post1($url,$arr_date);
		$result = json_decode($res,true);

		if($result['code'] == 0){  
			$_SESSION['code1'] = $yzm;
			$_SESSION['mobile1'] = $mobile;
			$_SESSION['uname1'] = $uname;
			$_SESSION['yzmtime1'] = time();
			$str = array(1,"验证码已发送");
			echo json_encode($str);
		}
		elseif($result['code']==9024){
			$str = array(0,"今日验证短信次数已用完");
			echo json_encode($str);
		}
		else{
			$str = array(0,"发送失败，请联系管理员");
			echo json_encode($str);
		}  
	}else{
		$_SESSION['code1'] = $yzm;
		$_SESSION['mobile1'] = $mobile;
		$_SESSION['uname1'] = $uname;
		$_SESSION['yzmtime1'] = time();
		$str = array(1,$yzm);
		echo json_encode($str);
	}	
}

function logout(){
	global $conf;
	$db = new db($conf);
	$sjcode = getRandom(12);
	$db->where("Id=".$_SESSION['userid'])->update("sc_member",array("sjcode"=>$sjcode));
	setcookie('username','',time()-1000,"");
	setcookie('password','',time()-1000,"");
	session_unset();
	success("退出成功");
}

}
?>